import {
	useUserStore
} from "../store/user"
const base_url = "http://8.136.49.19:8080"
const local_url = "http://localhost:8080"
export default {
	//封装uni.request()：
	request(options) {
		return new Promise((resolve, reject) => {
			const userStore = useUserStore()
			if (userStore.token) {
				options.header.token = userStore.token
			}
			//书写异步操作的代码
			uni.request({
				...options,
				success: res => {
					if (options.native) {
						resolve(res) //调取接口后返回的原生数据	
					}
					if (res.statusCode === 200) {
						resolve(res.data) //异步操作执行成功
					} else {
						console.log('请求的接口没有找到');
						reject(res) //异步操作执行失败
					}
				}
			})
		})
	},
	get(url, data = {}, options = {}) {
		options.url = base_url + url;
		options.data = data;
		options.method = 'GET';
		options.header = {
			'Content-Type': 'application/json',
			...options.header
		};
		return this.request(options)
	},
	post(url, data = {}, options = {}) {
		options.url = base_url + url;
		options.data = data
		options.method = 'POST';
		options.header = {
			'Content-Type': 'application/json',
			...options.header
		};
		return this.request(options)
	}

}